package com.yeshine.opendata.service.impl;

import com.yeshine.opendata.core.ServiceBase;
import com.yeshine.opendata.dao.IUserDao;
import com.yeshine.opendata.entity.SysDictionary;
import com.yeshine.opendata.entity.User;
import com.yeshine.opendata.service.IUserService;
import com.yeshine.opendata.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * Created by taohj on 2014/6/28.
 */
@Service
public class UserServiceImpl extends ServiceBase<User> implements IUserService {


    private String path = UploadConstantPath.USER_HEADIMAGE_PATH;


    private IUserDao dao;


    @Autowired
    public void setDao(IUserDao dao) {
        this.dao = dao;
    }

    @Override
    public Result<User> create(User user) {
        if(!StringUtils.hasLength(user.getPassword())){
            user.setPassword("123456");
        }

        user.setPassword(MD5Util.MD5Code(user.getPassword()));
        user.setStatus(new SysDictionary(151));
        user.setSex(new SysDictionary());
        user.setInTime(new Date());
        return new Result<User>(dao.insert(user) > 0);
    }


    @Override
    public Result<User> create(User user,HttpServletRequest request) throws IOException {


        MultipartFile file=user.getHeadImageFile();
        if (file.getSize()>0){
            String realpath=request.getSession().getServletContext().getRealPath(this.path);
            String filename=user.getId()+"_"+file.getOriginalFilename();
            File target=new File(realpath+"/"+filename);
            FileUtils.copy(file.getInputStream(),target);
        }
        return this.create(user);
    }


    @Override
    public Result<User> remove(User user) {
        return new Result<User>(dao.delete(user) > 0);
    }

    @Override
    public Result<User> modify(User user, HttpServletRequest request) {

        String path;
        String fileName = "";
        Result<User> result;

        MultipartFile file = user.getHeadImageFile();
        if (user.getHeadImageFile().getSize() > 0) {
            path = request.getSession().getServletContext().getRealPath(this.path);
            fileName = user.getId() + "_" + file.getOriginalFilename();
            File targetFile = new File(path, fileName);
            try {
                FileUtils.copy(file.getInputStream(),targetFile);
            } catch (IOException e) {
                e.printStackTrace();
                return new Result<User>(false, e.getMessage());
            }
            user.setHeadImage(this.path + fileName);
        }
        result = this.modify(user);
        request.getSession().setAttribute("user", user);
        return result;
    }


    @Override
    public Result<User> modify(User user) {
        return new Result<User>(dao.update(user) > 0);
    }


    @Override
    public Result<User> getById(Integer id) {
        return new Result<User>(dao.selectById(id));
    }

    @Override
    public Result<User> getList(User user) {
        return new Result<User>(dao.selectList(user));
    }

    @Override
    public Result<User> getByPage(PageResult<User> pageResult) {
        pageResult.setRows(dao.selectbyPage(pageResult));
        return new Result<User>(pageResult);
    }

    @Override
    public Result<User> activation(int id, String uid) {

        User user = this.getById(id).getTarget();
        if (user == null) {
            return new Result<User>(false, "该账号不存在");
        }
        String _uid = MD5Util.MD5Code(user.getId() + user.getUsername() + user.getEmail());
        if (_uid.equals(uid)) {
            user.setInTime(new Date());
            user.setStatus(new SysDictionary(152));
            if (!this.modify(user).isSuccess()) {
                return new Result<User>(false, "账号激活失败，地址不正确");
            }

        }
        return new Result<User>();
    }

    @Override
    public Result<User> login(User user) {
        user.setPassword(MD5Util.MD5Code(user.getPassword()));
        user = dao.login(user);
        if (user == null) {
            return new Result<User>(false, "账号或密码不正确");
        }
        if (user.getStatus() == null ||(user.getStatus().getId()!=152)) {
            return new Result<User>(false, "账号未激活");
        }
        user.setLastLoginTime(new Date());
        this.modify(user);
        return new Result<User>(user);
    }

    @Override
    public Result<User> password(Integer id, String password, String newpassword) {

        if (!StringUtils.hasLength(password)) {
            return new Result<User>(false, "原密码不能为空");
        }
        if (!StringUtils.hasLength(newpassword)) {
            return new Result<User>(false, "新密码不能为空");
        }
        User user = this.getById(id).getTarget();
        if (user == null) {
            return new Result<User>(false, "该账号不存在");
        }
        if (!MD5Util.MD5Code(password).equals(user.getPassword())) {
            return new Result<User>(false, "原密码不正确");
        }
        user.setPassword(MD5Util.MD5Code(newpassword));
        return this.modify(user);
    }
}
